package smetana.modules.ui.form.doc;

import com.google.inject.Inject;
import org.apache.pivot.collections.List;
import org.apache.pivot.collections.Sequence;
import org.apache.pivot.wtk.ActivityIndicator;
import org.apache.pivot.wtk.ApplicationContext;
import org.apache.pivot.wtk.Button;
import org.apache.pivot.wtk.Component;
import org.apache.pivot.wtk.ComponentKeyListener;
import org.apache.pivot.wtk.Keyboard.KeyLocation;
import org.apache.pivot.wtk.ListButton;
import org.apache.pivot.wtk.ListButtonSelectionListener;
import org.apache.pivot.wtk.ListView;
import org.apache.pivot.wtk.Span;
import org.apache.pivot.wtk.TableView;
import org.apache.pivot.wtk.TableViewSelectionListener;
import org.apache.pivot.wtk.TextInput;
import org.apache.pivot.wtk.TextInputTextListener;
import org.apache.pivot.wtk.content.ButtonDataRenderer;
import org.apache.pivot.wtk.content.ListViewItemRenderer;
import org.apache.pivot.wtkx.WTKX;
import smetana.domain.filter.PositionReportFilter;
import smetana.domain.join.PositionReport;
import smetana.domain.raw.Position;
import smetana.domain.raw.PositionGroup;
import smetana.modules.business.BusinessPivot;
import smetana.modules.business.BusinessPosition;
import smetana.modules.ui.form.AbstractPanel;
import smetana.modules.ui.model.CursorPosition;
import smetana.modules.ui.model.UIModel;
import smetana.domain.Id;
import smetana.util.StrangeQueue;
import smetana.util.Work;

/**
 *
 * @author dmitry.mamonov
 */
public class ProductsCatalogPanel extends AbstractPanel {
    @Inject
    BusinessPosition businessPosition;

    @Inject
    BusinessPivot businessPivot;

    @Inject
    UIModel uiModel;

    @Inject
    CursorPosition cursorPosition;

    @WTKX
    TableView tableView;

    @WTKX
    ListButton fieldProductGroupFilter;

    @WTKX
    TextInput fieldProductNameFilter;

    @WTKX
    ActivityIndicator activityIndicator;

    PositionReportFilter filter = new PositionReportFilter();

    StrangeQueue strangeQueue = new StrangeQueue() {
        @Override
        protected void onActivityChange(final boolean active) {
            activityIndicator.setActive(active);
        }
    };

    //PositionReportFilter filterLast = null;
    public ProductsCatalogPanel() {
        super();
    }

    @Override
    public void init() {
        { //group filter
            List<PositionGroup> productGroupList = businessPivot.pivotListOfProductGroups();
            PositionGroup all = new PositionGroup(-1);
            all.setGroupName("<все-группы>");
            productGroupList.insert(all, 0);
            fieldProductGroupFilter.setListData(productGroupList);
            fieldProductGroupFilter.setSelectedIndex(0);
            fieldProductGroupFilter.setDataRenderer(new ButtonDataRenderer() {
                @Override
                public void render(Object data, Button button, boolean highlighted) {
                    PositionGroup pg = (PositionGroup) data;
                    String strDate = (pg == null) ? "" : pg.getGroupName();
                    super.render(strDate, button, highlighted);
                }
            });
            fieldProductGroupFilter.setItemRenderer(new ListViewItemRenderer() {
                @Override
                public void render(Object item, int index, ListView listView, boolean selected, boolean checked, boolean highlighted, boolean disabled) {
                    PositionGroup pg = (PositionGroup) item;
                    String strItem = (pg == null) ? "" : pg.getGroupName();
                    super.render(strItem, index, listView, selected, checked, highlighted, disabled);
                }
            });


            fieldProductGroupFilter.getListButtonSelectionListeners().add(new ListButtonSelectionListener() {
                @Override
                public void selectedIndexChanged(ListButton listButton, int previousSelectedIndex) {
                    PositionGroup productGroup = (PositionGroup) listButton.getSelectedItem();
                    filter = new PositionReportFilter(Id.get(productGroup), filter.getProductNameFilter());
                    activityIndicator.setActive(true);
                    reloadTableViewData();
                }
            });
        }

        { //name filter


            fieldProductNameFilter.getTextInputTextListeners().add(new TextInputTextListener() {
                long lastUpdate = System.currentTimeMillis();

                @Override
                public void textChanged(TextInput textInput) {
                    filter = new PositionReportFilter(filter.getProductGroupIdFilter(), textInput.getText());
                    lastUpdate = System.currentTimeMillis();
                    activityIndicator.setActive(true);
                    ApplicationContext.scheduleCallback(new Runnable() {
                        @Override
                        public void run() {
                            if (System.currentTimeMillis() - lastUpdate > 450L) {
                                reloadTableViewData();
                            }
                        }
                    }, 500L);
                }
            });
            fieldProductNameFilter.getComponentKeyListeners().add(new ComponentKeyListener.Adapter() {
                @Override
                public boolean keyPressed(Component component, int keyCode, KeyLocation keyLocation) {
                    if (keyCode == java.awt.event.KeyEvent.VK_ENTER) {
                        reloadTableViewData();
                        return true;
                    }
                    return false;
                }
            });
        }
        { //init table listeners
            tableView.getTableViewSelectionListeners().add(new TableViewSelectionListener.Adapter() {
                @Override
                public void selectedRangesChanged(TableView tableView, Sequence<Span> previousSelectedRanges) {
                    PositionReport positionReport = (PositionReport) tableView.getSelectedRow();
                    if (positionReport != null) {
                        cursorPosition.set(positionReport);
                    } else {
                        cursorPosition.set(null);
                    }
                }
            });

        }
        reloadTableViewData();
    }

    private void reloadTableViewData() {
        strangeQueue.push(new Work.Adapter() {
            List<PositionReport> tableViewData;

            @Override
            public void doWork() {
                tableViewData = businessPivot.pivotListOfPositionReports(filter);
            }

            @Override
            public void publishResults() {
                PositionReport selected = (PositionReport) tableView.getSelectedRow();
                tableView.setTableData(tableViewData);
                tableView.setSelectedIndex(tableViewData.indexOf(selected));

            }
        });
    }

    public Position getSelected() {
        PositionReport selected = (PositionReport) tableView.getSelectedRow();
        return selected;
    }
}
